package com.server.pojo;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.server.pojo.base.PojoBase;
import lombok.*;
import lombok.experimental.Accessors;
import org.hibernate.Hibernate;

import javax.persistence.*;
import java.math.BigDecimal;
import java.util.HashSet;
import java.util.Objects;
import java.util.Set;

@Getter
@Setter
@ToString

@Entity
@Accessors(chain = true)
@NoArgsConstructor
@AllArgsConstructor
@Table
public class Unit extends PojoBase {

    @Column(unique = true)
    String code;

    String name;
    String addr;
    String tel;
    String manager;
    String policeStation;

    @Transient
    Boolean isJoin = false;

    Boolean isCanJoin = true;

    @JsonIgnore
    @OneToMany(mappedBy = "unit",cascade = CascadeType.ALL,orphanRemoval = true)
    @ToString.Exclude
    Set<Alarm> alarms = new HashSet<>();

    @JsonIgnore
    @OneToMany(mappedBy = "unit",cascade = CascadeType.ALL)
    @ToString.Exclude
    Set<MobileAppUnit> mobileAppUnits = new HashSet<>();

    @Column(precision = 13, scale = 10)
    BigDecimal longitude;

    @Column(precision = 13, scale = 10)
    BigDecimal latitude;


    @PrePersist
    public void prePersist(){

    }

    @PreUpdate
    public void preUpdate(){

    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || Hibernate.getClass(this) != Hibernate.getClass(o)) return false;
        Unit unit = (Unit) o;
        return getId() != null && Objects.equals(getId(), unit.getId());
    }

    @Override
    public int hashCode() {
        return getClass().hashCode();
    }
}
